﻿$(function () {
    document.onkeydown = function (e) {
        e = e ? e : window.event;
        var keyCode = e.which ? e.which : e.keyCode;
        if (keyCode == 13) {
            Login();
        }
    }
})
$('#btnlogin').click(function () {
    Login();
});
function Login() {
    var name = $("#account").val();
    var pwd = $("#password").val();
    var code = $("#code").val();
    if (name == "" || pwd == "" || code == "") {
        FailedAlert('账号或密码或验证码不能为空！');
        return;
    }
    var passd = md5(pwd);
    var index = layer.load(0, { shade: false }); //0代表加载的风格，支持0-2
    $.post("/Login/Login", { userName: name, pwd: passd, cacheCode: code }, function (data) {
        if (data.code == 1) {
            var url = "/Home/Index";
            if (data.data != null && data.data != "") {
                url = data.data;
            }
            window.location.href = url;
        } else {
            var code = document.getElementById("valiCode")
            code.src = "/Login/GetValidataCode?time=" + (new Date()).getTime();
            FailedAlert(data.msg);
        }
        layer.close(index);
    })
}
//校验是否为空 长度  格式
function CheckData(userName, acount, pwd, repeatpwd, regcode) {
    var checked = false;
    if (userName == "") {
        document.getElementById("userName").focus();
        FailedAlert("用户名不能为空");
        return false;
    }
    else if (acount == "") {
        document.getElementById("acount").focus();
        FailedAlert("手机号不能为空");
        return false;
    }
    else if (pwd == "") {
        document.getElementById("pwd").focus();
        FailedAlert("密码不能为空");
        return false;
    }
    else if (repeatpwd == "") {
        document.getElementById("repeatpwd").focus();
        FailedAlert("重复密码不能为空");
        return false;
    }
    else if (regcode == "") {
        document.getElementById("regcode").focus();
        FailedAlert("验证码不能为空");
        return false;
    }

    //校验密码长度
    if (!valiDateLength(pwd)) {
        $("#pwd").focus();
        FailedAlert("密码长度不符合规则");
        return checked;

    }
    if (!validateStr(pwd)) {
        $("#pwd").focus();
        FailedAlert("必须包含数字字母");
        return checked;
    }

    //校验密码一致性
    if (pwd != repeatpwd) {
        $("#repeatpwd").focus();
        FailedAlert("密码不一致");
        return checked;
    }
    ////验证手机格式
    //if (!validatePhone(acount)) {
    //    $("#acount").focus();
    //    FailedAlert("手机格式不正确");
    //    return checked;
    //}
    return true;
}

$("#btnregiser").click(function () {
    var userName = $("#userName").val();
    var acount = $("#acount").val();
    var pwd = $("#pwd").val();
    var repeatpwd = $("#repeatpwd").val();
    var regcode = $("#regcode").val();

    //校验数据
    if (!CheckData(userName, acount, pwd, repeatpwd, regcode)) {
        return;
    }
    var newpwd = md5(pwd);
    var newrppwd = md5(repeatpwd);
    var index = layer.load(0, { shade: false }); //0代表加载的风格，支持0-2
    $.post("/Login/RegisterUser", { userName: userName, acount: acount, pwd: newpwd, repeatpwd: newrppwd, regcode: regcode }, function (data) {
        if (data.code == 1) {
            SuccessAlert("注册成功，去登录")
            window.location.href = "/Login/Index";
        }
        else {
            var code = document.getElementById("registerCode")
            code.src = "/Login/GetRegisterCode?time=" + (new Date()).getTime();
            FailedAlert(data.msg);
        }
        layer.close(index);
    });
});

$(function () {
    $("#valiCode").bind("click", function () {
        this.src = "../Login/GetValidataCode?time=" + (new Date()).getTime();
    });
});
$(function () {
    $("#registerCode").bind("click", function () {
        this.src = "../Login/GetRegisterCode?time=" + (new Date()).getTime();
    });
});

